//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcBubbleView.h"

@implementation smcBubbleView

#pragma mark helper

- (void) _setupComment
{
	if (_isComment)
	{
		// no need for chars left
		[_charsLeftLabel setText: @"comment"];
		// no icon
		[_theIcon setAlpha: 0.0f];
		// no text
		[self setText: @""];		
		// location
		[_locationField setLabel: @"to"];
		[_locationField setEnabled: NO];
		[_locationField setText: _commentTo];
		// text view return and arbitrary length
		[_textView setReturnAllowed: YES];
		[_textView setOnlySMSLength: NO];
	
	} else
	{
		// chars left
		[_charsLeftLabel setText: @"140 left"];
		// icon
		[_theIcon setAlpha: 1.0f];
		// location and text
		[_locationField setLabel: @"from"];
		[_locationField setEnabled: YES];
		[self setLocation: [[NSUserDefaults standardUserDefaults] stringForKey: @"location"]];
		[self setText: [[NSUserDefaults standardUserDefaults] stringForKey: @"text"]];
		// text view return and arbitrary length
		[_textView setReturnAllowed: NO];
		[_textView setOnlySMSLength: YES];
	}
}

#pragma mark What makes it a subclass

- (void) dealloc
{
	[_commentToURL release];
	[_commentTo release];
	[_charsLeftLabel release];
	[_textTraits release];
	[_locationField release];
	[_textView release];
	[super dealloc];
}

- (id) initWithFrame: (CGRect) rect
{
	self = [super initWithFrame: rect];
	if (self != nil)
	{
		_isComment = NO;
		_commentTo = nil;
		_commentToURL = nil;
		
		// set up text traits
		_textTraits = [[UITextTraits defaultTraits] retain];
		[_textTraits setAutoEnablesReturnKey: NO];
		[_textTraits setReturnKeyType: 0];
		
		// the chars left label
		CGRect tmp = CGRectMake(rect.size.width - 80, 20, 72, 20);		
		_charsLeftLabel = [[UITextLabel alloc] initWithFrame: tmp];
		[_charsLeftLabel setBackgroundColor: [UIView colorWithRed: 0.95f green: 0.95f blue: 0.95f alpha: 1.0f]];
		[_charsLeftLabel setText: @"140 left"];
		[_charsLeftLabel setCentersHorizontally: YES];
		
		// the text view
		rect.origin.x = rect.origin.y = 0;
		rect.origin.x += 10;
		rect.size.width -= 20;
		rect.origin.y += 40;
		rect.size.height -= 70;
		_textView = [[smcTextView alloc] initWithFrame: rect];	
		[_textView setDelegate: self];
		[_textView takeTraitsFrom: self];
		
		// the location field
		rect.origin.y += rect.size.height + 1;
		rect.size.height = 25;
		rect.size.width -= 36.0f;
		_locationField = [[smcTextField alloc] initWithFrame: rect];
		[_locationField setDelegate: self];
		[_locationField takeTraitsFrom: self];
		
		// the icon view
		CGRect thisRect;
		thisRect.size = CGSizeMake(36.0f, 36.0f);
		thisRect.origin.y = rect.origin.y + rect.size.height - thisRect.size.height;
		thisRect.origin.x = rect.origin.x + rect.size.width;
		_theIcon = [[UIImageView alloc] initWithFrame: thisRect];
		[_theIcon setImage: [UIImage applicationImageNamed: @"plus.png"]]; 
		[_theIcon setTapDelegate: self];
		
		
		[self addSubview: _charsLeftLabel];
		[self addSubview: _textView];
		[self addSubview: _locationField];
		[self addSubview: _theIcon];
	}

	return self;
}

- (void)drawRect:(struct CGRect) rect
{

	CGRect bgRect = rect;
		
	CGColorRef white = [UIView colorWithRed:1.0f green:1.0f blue:1.0f alpha: 1.0f];
	CGColorRef lightgrey = [UIView colorWithRed: 0.95f green: 0.95f blue: 0.95f alpha: 1.0f];
 	
	CGContextSetFillColorWithColor(UICurrentContext(), white);

    float radius = 20; 
    bgRect.origin.y += 15;
	bgRect.size.height -= 15;
	
	// big bubble
    UIBezierPath *bgPath = [UIBezierPath roundedRectBezierPath: bgRect
    	withRoundedCorners: 15
    	withCornerRadius: radius];
	[bgPath fill];                
		
	bgRect.origin.y -= 15;
	bgRect.size.height += 15;
	
	// pointer
	bgPath = [UIBezierPath bezierPath];
	float midX = bgRect.origin.x + bgRect.size.width / 2;
	float topY = bgRect.origin.y;
	float botY = topY + 15;
	[bgPath moveToPoint: CGPointMake(midX, topY)];
	[bgPath lineToPoint: CGPointMake(midX+25.0, botY)];
	[bgPath lineToPoint: CGPointMake(midX-25.0, botY)];
	[bgPath lineToPoint: CGPointMake(midX, topY)];
	[bgPath fill];
    
	CGContextSetFillColorWithColor(UICurrentContext(), lightgrey);
	
	// bottom
	bgRect.origin = CGPointMake (0.0f, [_textView frame].size.height + [_textView frame].origin.y - 1.0f);
	bgRect.size.height = rect.size.height - [_textView frame].size.height - [_textView frame].origin.y + 1.0f;
	bgPath = [UIBezierPath roundedRectBezierPath: bgRect
    	withRoundedCorners: 12
    	withCornerRadius: radius];
	[bgPath fill];
	
	// chars left
	bgRect = CGRectMake(rect.size.width - 94, 15, 94, 25);
	bgPath = [UIBezierPath roundedRectBezierPath: bgRect
    	withRoundedCorners: 6
    	withCornerRadius: radius];
	[bgPath fill];
	
	[super drawRect: rect];
}

#pragma mark smcTextView & smcTextField delegate

- (void) textViewReturnWithoutAllowance: (id) sender
{
	if (sender == _textView)// okay, then activate the location field...
		[_locationField becomeFirstResponder];
	else if (sender == _locationField)
		[_textView becomeFirstResponder];
}

- (void) textViewDidChange: (id) sender
{
	if (!_isComment)
	{
		// okay some text changed, update chars left label
		[_charsLeftLabel setText: [NSString stringWithFormat: @"%i left", 140 - [[_textView text] length]]];
	}
	
	// and call delegate
	if ([[self delegate] respondsToSelector: @selector(textForPostDidChange:)])
		[[self delegate] performSelector: @selector(textForPostDidChange:) withObject: nil];
}

- (BOOL) shouldShowKeyboardForView: (id) view
{
	//NSLog(@"show %@", view);
	// call delegate for keyboard
	if ([[self delegate] respondsToSelector: @selector(shouldShowKeyboardForView:)])
		[[self delegate] performSelector: @selector(shouldShowKeyboardForView:) withObject: view];
	
	return YES;
}

- (BOOL) shouldHideKeyboardForView: (id) view
{
	//NSLog(@"hide %@", view);	
	// call delegate for keyboard
	if ([[self delegate] respondsToSelector: @selector(shouldHideKeyboardForView:)])
		[[self delegate] performSelector: @selector(shouldHideKeyboardForView:) withObject: view];
	
	return YES;
}

#pragma mark Tap Delegate (_theIcon)

- (BOOL)view:(id)view handleTapWithCount: (int) count event: (struct __GSEvent *) event
{
	// call delegate
	if (view == _theIcon)
		if ([[self delegate] respondsToSelector: @selector(iconAddButtonTappedInBubbleView:)])
			[[self delegate] performSelector: @selector(iconAddButtonTappedInBubbleView:) withObject: view];
}

#pragma mark Set and get and action

- (void) forbidEditing
{
	[_textView setEditable: NO];
}

- (void) allowEditing
{
	[_textView setEditable: YES];
}

- (void) setIsComment: (BOOL) yn
{
	if (yn != _isComment)
	{
		_isComment = yn;
		[self _setupComment];
	}
}

- (BOOL) isComment
{
	return _isComment;
}

- (void) setCommentTo: (NSString *) to
{
	if (to != _commentTo)
	{
		[_commentTo release];
		_commentTo = [[NSString stringWithString: to] retain];
	}
}

- (NSString *) commentTo
{
	return _commentTo;
}

- (void) setCommentToURL: (NSURL *) toURL
{
	if (toURL != _commentToURL)
	{
		[_commentToURL release];
		_commentToURL = [toURL copy];
	}
}

- (NSURL *) commentToURL
{
	return _commentToURL;
}

- (void) setIcon: (UIImage *) img
{
	[_theIcon setImage: img];
}

- (void) setText: (NSString *) string
{
	[_textView setText: string];
}

- (NSString *) text
{
	return [_textView text];
}

- (void) setLocation: (NSString *) string
{
	[_locationField setText: string];
}

- (NSString *) location 
{
	return [_locationField text];
}

#pragma mark UITextTraits taker

- (id) textTraits
{
	// both text fields should call this for the traits
	return _textTraits;
}


@end
